package pers.sports.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import pers.sports.entity.Project;
import pers.sports.service.ProjectService;
import pers.sports.service.StudentService;
import pers.sports.vo.ProjectSexNum;
import pers.sports.vo.StorageUser;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/info")
public class ProjectController {

    private final ProjectService projectService;
    private final StudentService studentService;

    public ProjectController(ProjectService projectService, StudentService studentService) {
        this.projectService = projectService;
        this.studentService = studentService;
    }

    /**
     * Map格式  {"code":0,"msg":"","count":1000,"data":[{},{}}]}
     */
    @RequestMapping("/term")
    public Map<String, Object> findTerm() {
        Map<String, Object> map = new HashMap<>();
        List<HashMap> listmap = new ArrayList<>();
        List<Project> listproj = projectService.findAllProject();

        for (int i = 0; i < listproj.size(); i++) {
            Map<String, String> mapp = new HashMap<>();
            mapp.put("projectId", listproj.get(i).getProjectId().toString());
            mapp.put("projectName", listproj.get(i).getProjectName());
            mapp.put("projectStart", listproj.get(i).getProjectStart());
            mapp.put("projectEnd", listproj.get(i).getProjectEnd());
            mapp.put("projectTeacher", listproj.get(i).getProjectTeacher());
            ProjectSexNum psn = projectService.findProjectNumById(listproj.get(i).getProjectId().toString());
            Integer number = listproj.get(i).getProjectApply() / 2;
            mapp.put("projectApplyF", (number - psn.getFemale()) + "/" + number);
            mapp.put("projectApplyM", (number - psn.getMale()) + "/" + number);
            listmap.add((HashMap) mapp);
        }
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", listproj.size());
        map.put("data", listmap);
        return map;
    }

    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    private String apply(String projectId, HttpServletRequest request) {
        StorageUser user = (StorageUser) request.getSession().getAttribute("user");
        if (user == null) {
            return "请先登录，再进行报名操作！！";
        }
        String stuid = studentService.findStuIdByNum(user.getNum());
        projectId = projectId.replace("\"", "");
        if (projectService.findAlreadySignedUp(stuid, projectId) == 0) {
            Integer num = projectService.insertApplyInfo(stuid, projectId);
            if (num != 0) {
                return "报名成功，请耐心等待管理员审核。";
            } else {
                return "报名失败，请稍后再报名。";
            }
        } else {
            return "对不起，不能重复报名。";
        }

    }

    @RequestMapping("findAll")
    public Map<String, Object> findAll(){
        Map<String, Object> map = new HashMap<>();
        List<Project> listproj = projectService.findAll();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", listproj.size());
        map.put("data", listproj);
        return map;
    }

}
